﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using FilmBlowingMachine.PLC;

namespace FilmBlowingMachine.ViewModel
{
    class DashboardViewModel : ViewModelBase
    {
        public DashboardViewModel()
        {
            AS200 = new DeltaAS200("192.168.1.5", 502, 120);
            ConnectCommand = new RelayCommand(ConnectCmd);

            Random rand = new Random();//创建一个随机数生成器
            SeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "平均厚度",
                    Values = new ChartValues<double>(Enumerable.Repeat<double>(100, AS200.NumberOfHeatingPoints)),
                    LineSmoothness = 0
                },
                new LineSeries
                {
                    Title = "加热点",
                    Values = new ChartValues<ObservableValue>(Enumerable.Repeat(100, AS200.NumberOfHeatingPoints).Select(i => new ObservableValue(rand.Next(50, AS200.NumberOfHeatingPoints))))
                }
            };

            Labels = Enumerable.Range(1, AS200.NumberOfHeatingPoints).Select(i => i.ToString()).ToArray();
            YFormatter = value => value.ToString();
        }

        private Task t1;
        private CancellationTokenSource tokenSource;
        private DeltaAS200 aS200;
        private SeriesCollection seriesCollection;

        public string[] Labels { get; set; }
        public Func<double, string> YFormatter { get; set; }
        public SeriesCollection SeriesCollection
        {
            get { return seriesCollection; }
            set { seriesCollection = value; RaisePropertyChanged(); }
        }
        public DeltaAS200 AS200
        {
            get { return aS200; }
            set { aS200 = value; RaisePropertyChanged(); }
        }

        #region Command

        public RelayCommand ConnectCommand { get; set; }


        private void ConnectCmd()
        {
            if (t1 == null || t1.Status != TaskStatus.Running)
            {
                tokenSource = new CancellationTokenSource();
                t1 = new Task(UpdateChart, tokenSource.Token, TaskCreationOptions.LongRunning);
                t1.Start();
            }
            else
            {
                tokenSource.Cancel();
            }
        }
        #endregion

        private void UpdateUI()
        {
            for (int i = 0; i < SeriesCollection[1].Values.Count; i++)
            {
                SeriesCollection[1].Values[i] = new ObservableValue(AS200.MeasuringPoints[i].position);
            }
        }

        private void UpdateChart()
        {
            if (!AS200.Connect()) return;

            while (true)
            {
                if (tokenSource.Token.IsCancellationRequested)
                {
                    return;
                }

                AS200.GetMeasuringPoints();
                UpdateUI();
                Thread.Sleep(3000);
            }
        }
    }
}
